home *** CD-ROM | disk | FTP | other *** search
- /*****************************************************/
- /*
- ** EffectsToy.c
- **
- ** This tests out the music and sound effects drivers
- **
- ** © 1989-1995 Steve Hales. All Rights Reserved.
- **
- ** History -
- ** 8/6/91 Created
- ** 10/5/91 Modified for Think C 5.0
- ** 7/13/92 Modified for Rev 3 of the SoundMusicSys driver
- ** 12/28/92 Modified for Resume returning an error
- ** 3/6/92 Modified for SoundDone callback
- ** 6/23/94 Updated for CodeWarrior
- **
- */
- /*****************************************************/
- #include <Types.h>
- #include <Memory.h>
- #include <Quickdraw.h>
- #include <OSEvents.h>
- #include <Desk.h>
- #include <Events.h>
- #include <StandardFile.h>
- #include <Resources.h>
- #include <Windows.h>
- #include <Fonts.h>
- #include <TextEdit.h>
- #include <Menus.h>
- #include <Dialogs.h>
- #include <ToolUtils.h>
- #include <Sound.h>
-
- #if THINK_C
- #include <Think.h>
- #else
-
- #if 0
- #include <Traps.h>
- #include <StdLib.h>
- #include <ctype.h>
- #include <files.h>
- #include <fcntl.h>
- #include <Strings.h>
- #endif
-
-
- #define TRUE true
- #define FALSE false
- #define NIL 0L
-
- #endif
-
-
- #include "TestLibrary.h"
-
- #include "SoundMusicSystem.h"
-
-
- short int drawDebug;
- static WindowPtr theWindow;
- static Boolean doneFlag;
- static Boolean continueResume;
- static Boolean pauseMusicOnly = FALSE;
-
- static char theDate[] = __DATE__;
- static char theTime[] = __TIME__;
-
- static Boolean GetReadFileName(char *pName, short *pVRef, long *pType, short typeCount)
- {
- static Point anchor = {100, 7}; /* top-left */
- SFReply reply;
-
- SFGetFile(anchor, NIL, NIL, typeCount, (void *)pType, NIL, &reply);
-
- if (pType)
- {
- pType[0] = reply.fType;
- }
- BlockMove((void *)reply.fName, pName, 32L);
- *pVRef = reply.vRefNum;
- return(reply.good);
- }
-
- static void StartTheFileForPlayback(void)
- {
- char name[256];
- short theVRef;
- FSSpec theFile;
- OSErr theErr;
-
- if (GetReadFileName(name, &theVRef, 0L, -1))
- {
- theErr = FSMakeFSSpec(theVRef, 0L, (void *)name, &theFile);
- if (theErr == noErr)
- {
- theErr = StartFilePlayback(&theFile, SOUND_RATE_11k, 30000L);
- DPrint(drawDebug, "StartFilePlayback() returns %ld\r", (long)theErr);
- }
- else
- {
- DPrint(drawDebug, "Error in FSMakeFSSpec (%d)\r", theErr);
- }
- }
- }
-
-
- static pascal Boolean AreWeDone(short int sampleID)
- {
- return Button();
- }
-
- static void PrintKeyboardMenu()
- {
- DPrint(drawDebug, "\
- Press\r\
- '1' to play a song\r\
- '2' to end the song\r\
- '3' to play a song looped\r\
- '4' to pause, then play sound manager beep, then resume\r\
- '5' to play sound list\r\
- '6' to play random custom data\r\
- '7' to play buffer\r\
- '8' to play a file based sample\r\
- '9' to stop playing a file based sample\r\
- 'a' to auto fade master output out\r\
- 's' to auto fade master output in\r\
- 't' to determine CPU load\r\
- 'r' to toggle reverse\r\
- 'd' to toggle check sound done flag\r\
- 'i' to toggle intrepolation on or off.\r\
- 'o' to toggle between 11khz and 22khz output\r");
- DPrint(drawDebug, "\
- space to end all sound effects\r\
- return to end last sound effect\r\
- 'zxcvbnm' for different sound effects\r\
- 'e' for enveloped sound effect while holding mouse button\r\
- 'f' for enveloped sound effect to play loop 5 times\r\
- 'g' for enveloped sound effect forever\r\
- '-' decrease pitch of current sound\r\
- '=' increase pitch of current sound\r\
- '[' decrease current master volume\r\
- ']' increase current master volume\r\
- 'j' return FadeLevel\r\
- 'k' return Midi Clock\r\
- '?' to display this command list\r\
- 'q' to quit\r\
- ");
- }
-
- //'(' decrease sample volume\r\
- //')' increase sample volume\r\
-
- /* When the callback is called by SoundMusicSys, theID will be the ID of the sample that is done.
- ** For buffer playback ie. PlayTheSample, theID will be CUSTOM_PLAY_ID
- */
- static Boolean soundDone;
- static short restartSoundID = -1;
- static short soundDoneID = -1;
-
- static pascal void SoundCallBack(short theID)
- {
- if (theID == CUSTOM_PLAY_ID)
- {
- soundDone = TRUE;
- }
- if (theID == 10000)
- {
- restartSoundID = 10000;
- }
- soundDoneID = theID;
- }
-
- static pascal void SongCallBack(short theID)
- {
- // Debugger();
- }
-
-
- static void PlayDoubleBuffer(void)
- {
- Handle waveHandle;
- Ptr wavePtr;
- long size;
-
- waveHandle = GetResource('wave', 128);
- if (waveHandle)
- {
- HLock(waveHandle);
- wavePtr = *waveHandle;
-
- soundDone = FALSE;
- restartSoundID = -1;
- SetSoundDoneCallBack(SoundCallBack);
- #if 1
- BeginSoundLoop(10000, SOUND_RATE_DEFAULT, -1L, -1L);
- // BeginSound(10000, SOUND_RATE_DEFAULT);
- #if 0
- while (!IsThisSoundFXFinished(10000));
- size = TickCount() + 45;
- while (TickCount() < size);
- #endif
- EndSound(10000);
- #endif
- size = GetHandleSize(waveHandle) / 2;
- #if 1
- while (!Button())
- {
- soundDone = FALSE;
- PlayTheSample(wavePtr, size, SOUND_RATE_11k);
- while ((soundDone == FALSE) and (!Button())) {};
- soundDone = FALSE;
- PlayTheSample(wavePtr+size, size, SOUND_RATE_11k);
- while ((soundDone == FALSE) and (!Button())) {};
- }
- #endif
- EndSound(10000);
- restartSoundID = -1;
- HUnlock(waveHandle);
- ReleaseResource(waveHandle);
- }
- }
-
- // Performance testing code
-
- static void MeasureCPUPerformance(short int drawDebug)
- {
- long n_ticks;
- long a, sys_clock;
-
- n_ticks = TickCount() + 300L;
- sys_clock = 0;
- a = 4322;
- while (n_ticks > TickCount())
- {
- a = a * 2;
- a = a / 2;
- sys_clock++;
- }
-
- DPrint(drawDebug, "Executed %ld iterations of benchmark.\r", sys_clock);
- }
-
-
-
-
- void main()
- {
- short int theCount, currentSampleVolume;
- long theRate, theRateMult;
- long tempLong, beatCount;
- Boolean reverseSample, checkDone, intrep;
- Boolean fileStream;
- SoundQuality quality;
- short int lastSound, newSound;
- char *intrepString;
- OSErr theErr;
- EventRecord theEvents;
- short int theSounds[] = {10000, 10001, 10002, 10003, 10003,10004, 10005, 10006, 10007, 10011, SOUND_END};
- Ptr waveData;
- short saveVolume, currentVolume;
- Handle theSong, theMidi;
- short int maxSongVoices, maxEffectsVoices, maxNormalVoices;
- static SampleList
- theSoundList[] =
- {
- 10000, SOUND_RATE_11k, PLAY_ALL_SAMPLE,
- 10002, SOUND_RATE_22k, 40,
- 10004, SOUND_RATE_11k, PLAY_ALL_SAMPLE,
- 10001, SOUND_RATE_11k, PLAY_ALL_SAMPLE
- };
- /* NOTE: The order of initilizing the system seems very important when running under
- ** MultFinder!!!
- */
- #if THINK_C
- InitGraf(&thePort); /* set up screen port */
- #else
- InitGraf(&qd.thePort); /* set up screen port */
- #endif
- InitFonts(); /* set up font manager */
- InitWindows(); /* set up window stuff */
- InitMenus(); /* set up menu mgr */
- TEInit(); /* set up text editor for system use */
- InitDialogs(NIL); /* set up Dialog stuff */
- InitCursor(); /* put pointer shape in mouse */
-
- ResrvMem(maxSize);
- MaxApplZone(); /* force expansion */
- FlushEvents(everyEvent, 0); /* clear all events */
- for (theCount = 0; theCount < 10; theCount++)
- {
- MoreMasters();
- }
-
- waveData = NewPtr(1000L);
-
- drawDebug = DNew((void *)"\pEffects Toy");
-
- DPrint(drawDebug, "Effect Toy - \r\
- %s %s\r\
- Demonstration of SoundMusicSys driver\r\
- © Copyright 1989-1995 Steve Hales, All rights reserved\r", theDate, theTime);
- PrintKeyboardMenu();
-
- theWindow = DWindow(drawDebug);
- SetPort(theWindow);
-
- theRateMult = 0;
- doneFlag = FALSE;
- fileStream = FALSE;
- continueResume = FALSE;
-
- quality = jxHighQuality; // jxHighQuality; // jxIntrepBestHighQuality; // jxAnalyzeQuality
- maxSongVoices = 4;
- maxNormalVoices = 4;
- maxEffectsVoices = 2;
- theErr = InitSoundMusicSystem( maxSongVoices, // max song voices
- maxNormalVoices, // max normalized voices
- maxEffectsVoices, // max sound effects track
- quality);
- if (theErr)
- {
- DPrint(drawDebug, "Error %ld initilizing the Music Driver.\r", (long)theErr);
- DPrint(drawDebug, "\rClick to exit.\r");
- while (Button()) {};
- while(!Button()) {};
- doneFlag = TRUE;
- }
- else
- {
- RegisterSounds(theSounds, TRUE); /* TRUE - dont load, FALSE - load */
- intrep = checkDone = reverseSample = FALSE;
- }
- saveVolume = GetMasterVolume();
- currentVolume = saveVolume;
- currentSampleVolume = FULL_VOLUME;
- DPrint(drawDebug, "Current Volume %ld\r", (long)currentVolume);
- EndSoundList();
- while (doneFlag == FALSE)
- {
- PurgeAllSounds(maxSize);
- if (ServiceFilePlayback()) // This must be called to process file streaming
- {
- DPrint(drawDebug, "Read block\r");
- }
- if (soundDoneID != -1)
- {
- DPrint(drawDebug, "%ld\r", (long)soundDoneID);
- soundDoneID = -1;
- }
- if (restartSoundID != -1)
- {
- BeginSound(restartSoundID, SOUND_RATE_DEFAULT);
- restartSoundID = -1;
- }
- if (WaitNextEvent(everyEvent, &theEvents, 0L, NIL))
- {
- DEvent(&theEvents);
-
- if (continueResume)
- {
- DPrint(drawDebug, "Resuming SoundGod…\r");
- theErr = ResumeSoundMusicSystem();
- if (theErr == noErr)
- {
- continueResume = FALSE;
- }
- else
- {
- DPrint(drawDebug, "Error Resuming %ld\r", (long)theErr);
- }
- }
- switch(theEvents.what)
- {
- case app4Evt: /* Suspend/Resume Events */
- if ( ((theEvents.message >> 24L) & 0x00FF) == 0x01)
- {
- if (theEvents.message & 1) /* message field contains flags */
- {
- continueResume = TRUE;
- }
- else
- {
- /* suspend */
- PauseSoundMusicSystem();
- }
- }
- break;
- case autoKey:
- case keyDown:
- switch(theEvents.message & 0xFF)
- {
- case '?':
- case '/':
- PrintKeyboardMenu();
- break;
- case 'q':
- doneFlag = TRUE;
- break;
- case 'i':
- intrep = (intrep) ? FALSE : TRUE;
- DPrint(drawDebug, "Intrepolation now %s\r", (intrep) ? "ON" : "OFF");
- // break;
- case 'o':
- switch (quality)
- {
- case jxLowQuality:
- case jxIntrepLowQuality:
- case jxIntrepBestLowQuality:
- quality = (intrep) ? jxIntrepHighQuality : jxHighQuality;
- intrepString = (intrep) ? "22khz (High, Intrep)" : "22khz (High)";
- break;
- case jxHighQuality:
- case jxIntrepHighQuality:
- case jxIntrepBestHighQuality:
- quality = (intrep) ? jxIntrepLowQuality : jxLowQuality;
- intrepString = (intrep) ? "11khz (Low, Intrep)" : "11khz (Low)";
- break;
- }
- DPrint(drawDebug, "Quality is now: %s\r", intrepString);
- ChangeOuputQuality(quality);
- break;
- case '1':
- SetSongDoneCallBack(NIL); // Callback will happen even with LoadSong
- theErr = LoadSong(128);
- theErr = BeginSong(128);
- tempLong = GetSongLength();
- SetSongDoneCallBack(SongCallBack);
- DPrint(drawDebug, "Begining song. 1-3 second delay.\r");
- DPrint(drawDebug, "BeginSong Returns %ld - Length %lX\r", (long)theErr, tempLong);
- break;
- case '!':
- /* NOTE: Don't release the resources until the song is finished playing. Once you
- ** start a song playing, the song and midi resources will become locked.
- */
- theSong = GetResource('SONG', 128);
- if (theSong)
- {
- theMidi = GetResource('Midi', 128);
- if (theMidi)
- {
- SetSongDoneCallBack(SongCallBack);
- BeginSongFromMemory(128, theSong, theMidi, FALSE);
- }
- }
- DPrint(drawDebug, "Begining song. 1-3 second delay.\r");
- break;
- case '2':
- DPrint(drawDebug, "End Song.\r");
- EndSong();
- break;
- case '@':
- DPrint(drawDebug, "Begining change test\r");
- while (Button() == FALSE)
- {
- theErr = ChangeSystemVoices( maxSongVoices, // max song voices
- maxNormalVoices, // max normalized voices
- maxEffectsVoices); // max sound effects track
- BeginSong(128);
- DPrint(drawDebug, "Song Started\r");
- tempLong = TickCount() + (2 * 60);
- while ( (TickCount() < tempLong) && (Button() == FALSE) )
- {
- }
- theErr = ChangeSystemVoices(0, 1, 1);
- DPrint(drawDebug, "Voices changed (%ld)\r", (long)theErr);
- BeginSound(10005, SOUND_RATE_DEFAULT);
- while (IsThisSoundFXFinished(10005) == FALSE)
- {
- }
- }
- break;
- case '3':
- SetSongDoneCallBack(NIL);
- DPrint(drawDebug, "Looped song\r");
- BeginSongLooped(128);
- break;
- case '4':
- DPrint(drawDebug, "Pausing SoundGod…\r");
- PauseSoundMusicSystem();
- DPrint(drawDebug, "Beeping Sound Manager…\r");
- SysBeep(1);
- DPrint(drawDebug, "Click to continue…\r");
- while(Button()) {};
- while(!Button()) {};
- FlushEvents(mDownMask, 0); /* clear button events */
- continueResume = TRUE;
- break;
- case '5':
- BeginSoundList(theSoundList, 4);
- break;
- case '6':
- if (waveData)
- {
- for (theCount = 0; theCount < 1000; theCount++)
- {
- waveData[theCount] = Random();
- }
- PlayTheSampleWithID(waveData, 1000L, SOUND_RATE_11k, 2300);
- // PlayTheSample(waveData, 1000L, SOUND_RATE_11k);
- }
- break;
- case '7':
- PlayDoubleBuffer();
- break;
- case '8':
- StartTheFileForPlayback();
- fileStream = TRUE;
- break;
- case '9':
- EndFilePlayback();
- fileStream = FALSE;
- DPrint(drawDebug, "EndFilePlayback()\r");
- break;
- case '0':
- if (pauseMusicOnly)
- {
- ResumeMusicOnly();
- pauseMusicOnly = FALSE;
- }
- else
- {
- PauseMusicOnly();
- pauseMusicOnly = TRUE;
- }
- break;
- case 's':
- BeginSong(128);
- BeginMasterFadeIn(40);
- DPrint(drawDebug, "BeginMasterFadeIn\r");
- break;
-
- case 'a':
- BeginMasterFadeOut(40);
- DPrint(drawDebug, "BeginMasterFadeOut\r");
- // while (FadeLevel() > 0);
- // while (IsSongDone() == FALSE);
- // DPrint(drawDebug, "done.\r");
- break;
-
- case 'r':
- reverseSample = (reverseSample) ? FALSE : TRUE;
- DPrint(drawDebug, "Reverse now %s\r", (reverseSample) ? "TRUE" : "FALSE");
- break;
- case 'd':
- checkDone = (checkDone) ? FALSE : TRUE;
- DPrint(drawDebug, "CheckDone now %s\r", (checkDone) ? "TRUE" : "FALSE");
- break;
- case 0x0D:
- // DPrint(drawDebug, "Stop %ld\r", (long)lastSound);
- EndSound(lastSound);
- break;
- case 0x20:
- EndAllSound();
- restartSoundID = -1;
- break;
- case '(':
- currentSampleVolume -= 5;
- if (currentSampleVolume < 0)
- {
- currentSampleVolume = 0;
- }
- DPrint(drawDebug, "New Sample Volume %ld\r", (long)currentSampleVolume);
- ChangeSoundVolume(lastSound, currentSampleVolume);
- // SetSongVolume(currentSampleVolume);
- break;
- case ')':
- currentSampleVolume += 5;
- if (currentSampleVolume > FULL_VOLUME)
- {
- currentSampleVolume = FULL_VOLUME;
- }
- DPrint(drawDebug, "New Sample Volume %ld\r", (long)currentSampleVolume);
- ChangeSoundVolume(lastSound, currentSampleVolume);
- // SetSongVolume(currentSampleVolume);
- break;
- case ']':
- currentVolume += 5;
- if (currentVolume > FULL_VOLUME)
- {
- currentVolume = FULL_VOLUME;
- }
- SetMasterVolume(currentVolume);
- DPrint(drawDebug, "New Volume %ld\r", (long)currentVolume);
- break;
- case '[':
- currentVolume -= 5;
- if (currentVolume < 0)
- {
- currentVolume = 0;
- }
- SetMasterVolume(currentVolume);
- DPrint(drawDebug, "New Volume %ld\r", (long)currentVolume);
- break;
- case '=':
- theRateMult += 500L << 16L;
- goto changeRate;
- case '-':
- theRateMult -= 500L << 16L;
- changeRate:
- DPrint(drawDebug, "New rate modifier: %ld of Sound %ld\r", (long)theRateMult, (long)lastSound);
- if (fileStream)
- {
- ChangeFilePlaybackRate(SOUND_RATE_7k + theRateMult);
- }
- else
- {
- theRate = GetSoundDefaultRate(lastSound) + theRateMult;
- ChangeSoundPitch(lastSound, theRate);
- }
- break;
- case 'k':
- beatCount = 0;
- while ((IsSongDone() == FALSE) && (Button() == FALSE) )
- {
- tempLong = GetCurrentMidiClock();
- theRate = GetNextBeatMidiClock();
- theRateMult = GetCurrentMidiBeat();
- if (beatCount != theRateMult)
- {
- beatCount = theRateMult;
- DPrint(drawDebug, "MIdi Clock %lX %lX %lX %lX\r", tempLong, theRateMult,
- theRate, GetCurrentTempo());
- }
- }
- break;
- case 'j':
- DPrint(drawDebug, "FadeLevel %ld\r", (long)FadeLevel());
- break;
- case 'f':
- BeginSoundEnvelope(10006, SOUND_RATE_DEFAULT, 5);
- lastSound = 10006;
- break;
- case 'e':
- lastSound = 10006;
- BeginSoundEnvelopeProc(10006, SOUND_RATE_DEFAULT, AreWeDone);
- break;
- case 'g':
- BeginSoundEnvelope(10003, SOUND_RATE_DEFAULT, -1);
- lastSound = 10003;
- break;
- case 'z':
- newSound = 10000;
- goto playSoundEffect;
- case 'u':
- newSound = 10011;
- goto playSoundEffect;
- case 'x':
- newSound = 10001;
- goto playSoundEffect;
- case 'c':
- newSound = 10002;
- goto playSoundEffect;
- case 'v':
- newSound = 10003;
- goto playSoundEffect;
- case 'b':
- newSound = 10004;
- goto playSoundEffect;
- case 'n':
- newSound = 10005;
- goto playSoundEffect;
- case 'm':
- newSound = 10007;
- playSoundEffect:
- // EndSound(lastSound);
- lastSound = newSound;
- restartSoundID = -1;
- ChangeSoundVolume(newSound, currentSampleVolume);
- SetSoundDoneCallBack(SoundCallBack);
- // DPrint(drawDebug, "Sound effect!\r");
- if (reverseSample == FALSE)
- {
- BeginSound(newSound, SOUND_RATE_DEFAULT);
- }
- else
- {
- BeginSoundReverse(newSound, SOUND_RATE_DEFAULT);
- }
- if (checkDone)
- {
- DPrint(drawDebug, "Waiting…");
- while ( (IsSoundFXFinished() == FALSE) && (Button() == FALSE) ) {};
- DPrint(drawDebug, " done.\r");
- }
- break;
- case 't':
- DPrint(drawDebug, "Testing CPU...\r\r");
- MeasureCPUPerformance(drawDebug);
- DPrint(drawDebug, "\r");
- break;
- case 'h':
- maxSongVoices ^= 4;
- DPrint(drawDebug, "%d %d %d = ", maxSongVoices, maxNormalVoices, maxEffectsVoices);
- theErr = ChangeSystemVoices( maxSongVoices, // max song voices
- maxNormalVoices, // max normalized voices
- maxEffectsVoices); // max sound effects track
- DPrint(drawDebug, "%ld\r", (long)theErr);
- break;
- }
- break;
- }
- }
- }
- if (waveData)
- {
- DisposPtr(waveData);
- }
- SetMasterVolume(saveVolume);
- FinisSoundMusicSystem();
- FlushEvents(everyEvent, 0); /* clear all events */
- }
-
- /* EOF of EffectsToy.c
- */
-
-